package com.retail.common.handler;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @Author：开心就好
 * @Description:
 * @Date:2024/4/5
 */
@Component
public class SentinelException implements BlockExceptionHandler {


    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception {

        Assert.isTrue(e instanceof FlowException, "请求已限流");
        Assert.isTrue(e instanceof DegradeException, "请求降级");
        Assert.isTrue(e instanceof ParamFlowException, "热点参数限流");
        Assert.isTrue(e instanceof AuthorityException, "请求权限");

    }
}
